unit Unit43; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, DbCtrls,
  StdCtrls, Unit36, Unit1, sqldb;

type

  { TForm43 }

  TForm43 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form43: TForm43; 

implementation

{ TForm43 }

procedure TForm43.FormShow(Sender: TObject);
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  ComboBox1.Text := '';
  
  Button1.SetFocus;
end;

procedure TForm43.Button1Click(Sender: TObject);
begin
  Form36.ShowModal;

  if Form36.codigo <> '' then
  begin
    Edit1.Text := Form36.codigo;
    Edit2.Text := Form36.nombre;

    Edit3.SetFocus;
  end
  else
    Button1.SetFocus;
end;

procedure TForm43.Button2Click(Sender: TObject);
var
  QueryString : string;
  codigo_pago : string;
  saldo_cliente : string;
  dinero_pago : real;
  codigo_venta : string;
  saldo_venta : real;
  cantidad_ventas : integer;

begin
  if (Edit1.Text <> '') and (Edit3.Text <> '') then
  begin
    QueryString := 'insert into pago(pgo_cliente, pgo_monto, pgo_descripcion, pgo_tipo, pgo_sucursal) '
      + 'values('+ Edit1.Text + ', ' + Edit3.Text + ', ''' + Edit4.Text + ''', '''
      + ComboBox1.Text + ''', '+ registro.codigo_sucursal + ')';
      
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    if SQLQuery1.Active = True then
      SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'select max(pgo_cdg) from pago where pgo_cliente = ' + Edit1.Text;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      codigo_pago := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;

    SQLQuery1.Close;

    QueryString := 'insert into resumen_cuenta(rsm_cliente, rsm_descripcion, rsm_operacion, rsm_haber) '
      + 'values(' + Edit1.Text + ', ''PAGO CUENTA (' + ComboBox1.Text + ', ' + Edit4.Text + ')'', ' + codigo_pago
      + ', ' + Edit3.Text + ')';

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    dinero_pago := strtofloat(Edit3.text);

    SQLQuery1.SQL.Text := 'select count(*) from venta where vta_saldo > 0 and vta_cliente = ' + Edit1. Text;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      cantidad_ventas := strtoint(SQLQuery1.Fields[0].AsString);
      SQLQuery1.Next;
    end;

    SQLQuery1.Close;

    while (dinero_pago > 0) and (cantidad_ventas > 0) do
    begin
      SQLQuery1.SQL.Text := 'select min(vta_cdg) from venta where vta_saldo > 0 and vta_cliente = ' + Edit1.text;

      SQLQuery1.Open;

      while not SQLQuery1.EOF do
      begin
        codigo_venta := SQLQuery1.Fields[0].AsString;
        SQLQuery1.Next;
      end;

      SQLQuery1.Close;

      SQLQuery1.SQL.Text := 'select vta_saldo from venta where vta_cdg = ' + codigo_venta;

      SQLQuery1.Open;

      while not SQLQuery1.EOF do
      begin
        saldo_venta := strtofloat(SQLQuery1.Fields[0].AsString);
        SQLQuery1.Next;
      end;

      SQLQuery1.Close;

      if dinero_pago >= saldo_venta then
      begin
        dinero_pago -= saldo_venta;

        QueryString := 'update venta set vta_saldo = 0 where vta_cdg = ' + codigo_venta;

        Form1.PQConnection1.ExecuteDirect('begin work;');
        Form1.PQConnection1.ExecuteDirect(QueryString);
        Form1.PQConnection1.ExecuteDirect('commit work;');
      end
      else
      begin
        saldo_venta -= dinero_pago;
        dinero_pago := 0;

        QueryString := 'update venta set vta_saldo = ' + floattostr(saldo_venta) + ' where vta_cdg = ' + codigo_venta;

        Form1.PQConnection1.ExecuteDirect('begin work;');
        Form1.PQConnection1.ExecuteDirect(QueryString);
        Form1.PQConnection1.ExecuteDirect('commit work;');
      end;

      cantidad_ventas -= 1;
    end;

    ShowMessage('Pago Exitoso');
    
    Close;
  end;
end;

procedure TForm43.Button3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm43.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
end;

initialization
  {$I unit43.lrs}

end.

